package com.study.modules.saas.controller;

import com.study.Util.CommonResponse;
import com.study.annotation.RepeatSubmit;
import com.study.log.annotation.Log;
import com.study.modules.saas.repository.enums.MenuType;
import com.study.modules.saas.repository.model.MenuDO;
import com.study.modules.saas.service.MenuService;
import com.study.vo.MenuRequest;
import com.study.vo.MenuResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 菜单管理
 * @ClassName: SaasMenuController
 * @Author: hupengming
 * @CreateTime: 2022-08-24 11:46
 */
@Api("菜单管理")
@Slf4j
@RestController
@RequestMapping("sa/menus")
public class SaasMenuController {

    @Autowired
    MenuService menuService;

    @Log("添加菜单")
    @ApiOperation("添加菜单")
    @PostMapping()
    @PreAuthorize("@sa.hasPermission('menu:add')")
    public CommonResponse<Integer> addMenu(@RequestBody MenuRequest request) {
        return CommonResponse.success(menuService.createMenu(request));
    }

    @Log("菜单分页")
    @ApiOperation("菜单列表")
    @GetMapping()
//    @PreAuthorize("@sa.hasPermission('menu:list')")
    @RepeatSubmit
    public CommonResponse<List<MenuDO>> page(@RequestParam(value = "button", required = false) boolean button,
                                             @RequestParam(value = "name",required = false) String name,
                                             @RequestParam(value = "type",required = false) MenuType type) {
        List<MenuDO> menuPage = menuService.getMenuPage(button,name,type);
        return CommonResponse.success(menuPage);
    }

    @Log("更新菜单")
    @ApiOperation("更新菜单")
    @PutMapping("/{id}")
    @PreAuthorize("@sa.hasPermission('menu:update')")
    public CommonResponse update(@RequestBody MenuRequest request,@PathVariable Integer id){

        return CommonResponse.success();
    }


    @Log("删除菜单")
    @ApiOperation("删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("@sa.hasPermission('menu:delete')")
    public CommonResponse deleted(@PathVariable Integer id){
        return CommonResponse.success(menuService.deletedMenu(id));
    }

    @ApiOperation("当前用户全部菜单")
    @GetMapping("/online")
    public CommonResponse online(@RequestParam(value = "button" ,defaultValue = "true") boolean button ){
        return CommonResponse.success(menuService.online(button));
    }

    @ApiOperation("菜单下拉框")
    @GetMapping("/choose")
    public CommonResponse choose(@ApiParam("是否开启树状结构") @RequestParam(required = false,defaultValue = "true") boolean tree){

        List<MenuDO> list = menuService.choose(tree);
        List<MenuResponse> responses = new ArrayList<>(list.size());
        for (MenuDO menuDO : list) {
            MenuResponse re = new MenuResponse();
            List<MenuDO> childList = menuDO.getChildList();

            if(!childList.isEmpty()){
                List<MenuResponse> collect = childList.stream().map(m -> {
                    MenuResponse r = new MenuResponse();
                    r.setKey(m.getId());
                    r.setTitle(m.getName());
                    r.setLeft(Boolean.FALSE);
                    r.setChildList(null);
                    return r;
                }).collect(Collectors.toList());
                re.setLeft(Boolean.FALSE);
                re.setChildList(collect);
            }else{
                re.setLeft(Boolean.TRUE);
            }
            re.setTitle(menuDO.getName());
            re.setKey(menuDO.getId());
            responses.add(re);
        }
        return CommonResponse.success(responses);
    }

}
